﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DB_QLSV.BusinessClassLibrary
{
    public class SinhVienBUS
    {
        private static QLSVDataContext db = new QLSVDataContext();
        public static bool ThemSinhVien(SinhVien SinhVien)
        {
            try
            {
                db.SinhViens.InsertOnSubmit(SinhVien);
                db.SubmitChanges();
            }
            catch
            {
                //conflict primary key - the item is exist
                return false;
            }
            return true;
        }
        public static bool XoaSinhVien(string idSinhVien)
        {
            try
            {
                SinhVien old = db.SinhViens.Single(kh => kh.mssv == idSinhVien);
                db.SinhViens.DeleteOnSubmit(old);
                db.SubmitChanges();
            }
            catch
            {
                // the item is not exist
                return false;
            }
            return true;
        }
        public static bool CapNhatSinhVienHoc(string id, SinhVien sinhVien)
        {
            try
            {
                SinhVien old = db.SinhViens.Single(kh => kh.mssv == id);
                old.cmnd = sinhVien.cmnd;
                old.gioitinh = sinhVien.gioitinh;
                old.hinhanh = sinhVien.hinhanh;
                old.hktt = sinhVien.hktt;
                old.hoten = sinhVien.hoten;
                old.lop = sinhVien.lop;
                old.ngaysinh = sinhVien.ngaysinh;
                old.nguyenquan = sinhVien.nguyenquan;

                db.SubmitChanges();
            }
            catch
            {
                // the item is not exist
                return false;
            }
            return true;
        }
        public static List<SinhVien> LayDanhSach()
        {
            return db.SinhViens.ToList();
        }
        public static List<SinhVien> LayDanhSachTheoMaLop(String malop)
        {
            var list = from kq in db.SinhViens where kq.lop == malop select(kq);
            //List<SinhVien>
            return null;
        }
        //get subjects depend on LopObject
        public static List<StudentManagement.DataGVStudentObject> LayDanhSach(Lop lopObject)
        {
            List<StudentManagement.DataGVStudentObject> lst = new List<StudentManagement.DataGVStudentObject>();
            var query = from student in db.SinhViens
                        from branch in db.Nganhs
                        where student.lop == lopObject.malop && branch.masonganh == lopObject.nganh
                        select new
                        {
                            student.mssv,
                            student.hoten,
                            student.gioitinh,
                            student.nguyenquan,
                            lopObject.tenlop,
                            branch.tennganh

                        };
            foreach (var item in query)
            {
                StudentManagement.DataGVStudentObject _new = new StudentManagement.DataGVStudentObject();
                _new.mssv = item.mssv;
                _new.hoten = item.hoten;
                if (item.gioitinh == 0)
                _new.gioitinh = ConstantString.NAM;
                else
                    _new.gioitinh = ConstantString.NU;
                
                _new.nguyenquan = item.nguyenquan;
                _new.tenlop = item.tenlop;
                _new.nganh = item.tennganh;
                lst.Add(_new);
            }
            if (lst.Count() > 0)
                return lst;
            return null;
        }
    }
}
